//数据模拟
import Mock from "mockjs";
Mock.setup({
  timeout: "500-1000",
});

/**
 * //获取请求参数
 * @param {*} options url
 * @returns params
 */
function getParams(options) {
  const paramsStr = /.+\?(.+)$/.exec(options.url)[1]; // 将 ? 后面的字符串取出来
  const paramsArr = paramsStr.split("&"); // 将字符串以 & 分割后存到数组中
  let paramsObj = {};
  // 将 params 存到对象中
  paramsArr.forEach((param) => {
    if (/=/.test(param)) {
      // 处理有 value 的参数
      let [key, val] = param.split("="); // 分割 key 和 value
      val = decodeURIComponent(val); // 解码
      val = /^\d+$/.test(val) ? parseFloat(val) : val; // 判断是否转为数字
      if (paramsObj.hasOwnProperty(key)) {
        // 如果对象有 key，则添加一个值
        paramsObj[key] = [].concat(paramsObj[key], val);
      } else {
        // 如果对象没有这个 key，创建 key 并设置值
        paramsObj[key] = val;
      }
    } else {
      // 处理没有 value 的参数
      paramsObj[param] = true;
    }
  });

  return paramsObj;
}

//商品标题

const { datas } = Mock.mock({
  "datas|120": [
    {
      "id|+1": 1,
      title: "短袖T恤男夏季潮流男装2023新款宽松半袖上衣纯棉卡通印花体恤衫T",
      cardTitel: "男士短袖衬衫",
      "price|19-200.1-2": 0,
      "quantity|80-500": 0,
      tags: ["先用后付", "48小时内发货"],
      address: "@city(true)",
      "image|2-5": ["@dataimage('500x350','')"],
    },
  ],
});

//获取商品列表
Mock.mock(RegExp("goods/list" + ".*"), "get", (options) => {
  const { page, size, sort, count } = getParams(options);
  const arr = [...datas];
  const total = arr.length;
  let sum = page * size; //需要获取的数据个数
  if (sum >= total) {
    sum = total;
  }
  let newArr;
  //判断排列方式
  if (sort === "price-up") {
    //价格降序
    newArr = arr.sort((a, b) => b.price - a.price);
  } else if (sort === "price-down") {
    //价格升序
    newArr = arr.sort((a, b) => a.price - b.price);
  } else if (sort === "sell") {
    //销售量
    newArr = arr.sort((a, b) => b.quantity - a.quantity);
  } else {
    //综合
    newArr = arr;
  }
  const list = newArr.slice(count, sum); //需要数据

  return Mock.mock({
    code: 0,
    msg: "获取成功",
    data: {
      list,
      total,
    },
  });
});

//根据id获取商品
Mock.mock(RegExp("goods/one" + ".*"), "get", (options) => {
  const { id } = getParams(options);

  const data = datas.filter((item) => item.id === id);

  return Mock.mock({
    code: 0,
    msg: "获取成功",
    data: data[0],
  });
});

//收藏商品
Mock.mock("goods/collect", "post", (options) => {
  const { id, collect } = JSON.parse(options.body);

  datas.forEach((item, i) => {
    if (item.id === id) {
      datas[i].collect = collect;
    }
  });

  return Mock.mock({
    code: 0,
    msg: "获取成功",
    data: collect,
  });
});

//获取收藏列表
Mock.mock(RegExp("goods/collect" + ".*"), "get", (options) => {
  const { page, size, sort, count } = getParams(options);
  const arr = datas.filter((item) => item.collect);
  const total = arr.length;
  let sum = page * size; //需要获取的数据个数
  if (sum >= total) {
    sum = total;
  }
  let newArr;
  //判断排列方式
  if (sort === "price-up") {
    //价格降序
    newArr = arr.sort((a, b) => b.price - a.price);
  } else if (sort === "price-down") {
    //价格升序
    newArr = arr.sort((a, b) => a.price - b.price);
  } else if (sort === "sell") {
    //销售量
    newArr = arr.sort((a, b) => b.quantity - a.quantity);
  } else {
    //综合
    newArr = arr;
  }
  const list = newArr.slice(count, sum); //需要数据

  return Mock.mock({
    code: 0,
    msg: "获取成功",
    data: {
      list,
      total,
    },
  });
});

//获取购物车商品
Mock.mock(RegExp("goods/cart" + ".*"), "get", (options) => {
  const { arr } = getParams(options);
  const newArr = JSON.parse(arr);
  const data = datas.filter((item) => newArr.includes(item.id + ""));

  return Mock.mock({
    code: 0,
    msg: "获取成功",
    data,
  });
});

//搜索商品
Mock.mock(RegExp("goods/search" + ".*"), "get", (options) => {
  const { val } = getParams(options);

  const stri = Math.random() + "";
  const is = +stri.substring(2, 3);
  const num = +stri.substring(2, 4);
  let data;
  if (is > 5) {
    data = [];
  } else {
    data = datas.slice(0, num);
  }
  return Mock.mock({
    code: 0,
    msg: "获取成功",
    data: data,
  });
});
